ActiveAdmin.register_page "StockBPDashboard" do
  menu priority: 40, label: '买点看板'

  content title: "" do
    div class: "总览" do
      span class: "blank_slate" do
        span "昨日买点数量：#{Stock.is_buy_point?.count}"
      end
    end
    stock_buy_points = Stock.is_buy_point?
    # 找到所有 stock_buy_points 的板块
    gn_base50_name = BlockFlow.where(cate_type: "N", mark: "base").last(50).pluck(:name)
    hy_base50_name = BlockFlow.where(cate_type: "I", mark: "base").last(50).pluck(:name)

    blocks = stock_buy_points.map { |stock| stock.blocks }.flatten.uniq
    blocks_name = blocks.map { |block| block.name } - ['融资融券', '深股通', '沪股通', '国企改革', '电子商务', '西部大开发', '一带一路'] # 排除一些不相关的板块
    blocks_count = {}

    blocks_name_50 = blocks_name & (gn_base50_name + hy_base50_name)

    blocks_name_50.each do |name|
      count = stock_buy_points.select { |stock| stock.blocks.pluck(:name).include?(name) }.count
      blocks_count[name] = count
    end # 计算每个板块的股票数量

    blocks_count = blocks_count.sort_by { |name, count| -count }.to_h # 按数量降序排序
    blocks_name_50 = blocks_count.keys.first 15 # 排序后的板块名称
    block_counts = blocks_count.values.first 15 # 排序后的板块对应的股票数量

    div id: 'block_count', style: "width: 2000px; height: 300px;"
    script <<~JS.html_safe
      var block_countDom = document.getElementById('block_count');
      var myChart = echarts.init(block_countDom);
      var option = {
        xAxis: {
          type: 'category',
          data: #{raw blocks_name_50.to_json}
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data:  #{raw block_counts.to_json},
            type: 'bar'
          }
        ] 
      };
      option && myChart.setOption(option);
    JS
    redis_stocks = Parallel.map(stock_buy_points, in_processes: 1, progress: false) do |stock|
      JSON.parse $Redis.get(stock.symbol)
    end
    choice = stock_buy_points
    # choice = stock_buy_points.select { |stock| stock["ths_flow"].to_i > 0 && stock["year_count"].to_i > 0 }
    stock_up5 = choice.select { |stock| redis_stocks.find { |x| x["code"] == stock.symbol }["changepercent"].to_f > 5 }
    stock_up3_5 = choice.select { |stock| (redis_stocks.find { |x| x["code"] == stock.symbol }["changepercent"].to_f > 3) && (redis_stocks.find { |x| x["code"] == stock.symbol }["changepercent"].to_f <= 5) }
    stock_up2_3 = choice.select { |stock| (redis_stocks.find { |x| x["code"] == stock.symbol }["changepercent"].to_f > 2) && (redis_stocks.find { |x| x["code"] == stock.symbol }["changepercent"].to_f <= 3) }
    stock_up1_2 = choice.select { |stock| (redis_stocks.find { |x| x["code"] == stock.symbol }["changepercent"].to_f > 1) && (redis_stocks.find { |x| x["code"] == stock.symbol }["changepercent"].to_f <= 2) }
    stock_upf3_1 = choice.select { |stock| (redis_stocks.find { |x| x["code"] == stock.symbol }["changepercent"].to_f > -3) && (redis_stocks.find { |x| x["code"] == stock.symbol }["changepercent"].to_f <= 1) }
    stock_downf3 = choice.select { |stock| (redis_stocks.find { |x| x["code"] == stock.symbol }["changepercent"].to_f < -3)}

    3.times do |time|
      columns do
        (1..5).each do |i|
          name = blocks_name_50[time * 5 + i - 1]
          stocks_b_p = stock_buy_points.select { |stock| stock.blocks.pluck(:name).include?(name) }
          column do
            panel "#{name}板块" do
              table_for stocks_b_p.sort_by { |stock| redis_stocks.find { |x| x["code"] == stock.symbol }["changepercent"] }.reverse do
                column :name
                column :symbol do |stock|
                  link_to "#{stock.symbol}", admin_stock_path(stock.id)
                end
                column :yc do |stock|
                  stock.year_count
                end
                column :vch do |stock|
                  stock.volume_change
                end
                column :change do |stock|
                  redis_stocks.find { |x| x["code"] == stock.ts_code[0..5] }["changepercent"].to_f.round(1)
                end
                column :flow do |stock|
                  link_to "#{stock.ths_flow}", "https://stockpage.10jqka.com.cn/#{stock.symbol}/funds/#funds_sszjlx", target: '_blank', rel: "noopener noreferrer"
                end
              end
            end
          end
        end
      end
    end
    columns do
      column do
        panel "涨幅 1%～2%" do
          table_for stock_up1_2.reverse do
            column '【name】' do |stock|
              code = %w(5 6 9).include?(stock.symbol[0]) ? "sh#{stock.symbol}" : "sz#{stock.symbol}"
              link_to "#{stock.name}", "https://finance.sina.com.cn/realstock/company/#{code}/nc.shtml", target: '_blank', rel: "noopener noreferrer"
            end
            column :symbol do |stock|
              link_to "#{stock.symbol}", admin_stock_path(stock.id)
            end
            column :ch do |stock|
              (JSON.parse $Redis.get(stock.symbol))["changepercent"].round(1)
            end
            column :yc do |stock|
              stock.year_count
            end
            column :vch do |stock|
              stock.volume_change
            end
            column :flow do |stock|
              link_to "#{stock.ths_flow}", "https://stockpage.10jqka.com.cn/#{stock.symbol}/funds/#funds_sszjlx", target: '_blank', rel: "noopener noreferrer"
            end
            column :block do |stock|
              "#{stock.blocks.pluck(:name) & gn_base50_name}" + "#{stock.blocks.pluck(:name) & hy_base50_name}"
            end
          end
        end
      end
      column do
        panel "涨幅 2%～3%" do
          table_for stock_up2_3.reverse do
            column '【name】' do |stock|
              code = %w(5 6 9).include?(stock.symbol[0]) ? "sh#{stock.symbol}" : "sz#{stock.symbol}"
              link_to "#{stock.name}", "https://finance.sina.com.cn/realstock/company/#{code}/nc.shtml", target: '_blank', rel: "noopener noreferrer"
            end
            column :symbol do |stock|
              link_to "#{stock.symbol}", admin_stock_path(stock.id)
            end
            column :ch do |stock|
              (JSON.parse $Redis.get(stock.symbol))["changepercent"].round(1)
            end
            column :yc do |stock|
              stock.year_count
            end
            column :vch do |stock|
              stock.volume_change
            end
            column :flow do |stock|
              link_to "#{stock.ths_flow}", "https://stockpage.10jqka.com.cn/#{stock.symbol}/funds/#funds_sszjlx", target: '_blank', rel: "noopener noreferrer"
            end
            column :block do |stock|
              "#{stock.blocks.pluck(:name) & gn_base50_name}" + "#{stock.blocks.pluck(:name) & hy_base50_name}"
            end
          end
        end
      end
    end
    columns do
      column do
        panel "涨幅 -3%～1%" do
          table_for stock_upf3_1.reverse do
            column '【name】' do |stock|
              code = %w(5 6 9).include?(stock.symbol[0]) ? "sh#{stock.symbol}" : "sz#{stock.symbol}"
              link_to "#{stock.name}", "https://finance.sina.com.cn/realstock/company/#{code}/nc.shtml", target: '_blank', rel: "noopener noreferrer"
            end
            column :symbol do |stock|
              link_to "#{stock.symbol}", admin_stock_path(stock.id)
            end
            column :ch do |stock|
              (JSON.parse $Redis.get(stock.symbol)|| '{}')["changepercent"]&.round(1) if $Redis.get(stock.symbol)
            end
            column :yc do |stock|
              stock.year_count
            end
            column :vch do |stock|
              stock.volume_change
            end
            column :flow do |stock|
              link_to "#{stock.ths_flow}", "https://stockpage.10jqka.com.cn/#{stock.symbol}/funds/#funds_sszjlx", target: '_blank', rel: "noopener noreferrer"
            end
            column :block do |stock|
              "#{stock.blocks.pluck(:name) & gn_base50_name}" + "#{stock.blocks.pluck(:name) & hy_base50_name}"
            end
          end
        end
      end
      column do
        panel "涨幅 小于 -3%" do
          table_for stock_downf3.reverse do
            column '【name】' do |stock|
              code = %w(5 6 9).include?(stock.symbol[0]) ? "sh#{stock.symbol}" : "sz#{stock.symbol}"
              link_to "#{stock.name}", "https://finance.sina.com.cn/realstock/company/#{code}/nc.shtml", target: '_blank', rel: "noopener noreferrer"
            end
            column :symbol do |stock|
              link_to "#{stock.symbol}", admin_stock_path(stock.id)
            end
            column :ch do |stock|
              (JSON.parse $Redis.get(stock.symbol))["changepercent"].round(1)
            end
            column :yc do |stock|
              stock.year_count
            end
            column :vch do |stock|
              stock.volume_change
            end
            column :flow do |stock|
              link_to "#{stock.ths_flow}", "https://stockpage.10jqka.com.cn/#{stock.symbol}/funds/#funds_sszjlx", target: '_blank', rel: "noopener noreferrer"
            end
            column :block do |stock|
              "#{stock.blocks.pluck(:name) & gn_base50_name}" + "#{stock.blocks.pluck(:name) & hy_base50_name}"
            end
          end
        end
      end
    end
    columns do
      column do
        panel "涨幅 3%～5%" do
          table_for stock_up3_5.reverse do
            column '【name】' do |stock|
              code = %w(5 6 9).include?(stock.symbol[0]) ? "sh#{stock.symbol}" : "sz#{stock.symbol}"
              link_to "#{stock.name}", "https://finance.sina.com.cn/realstock/company/#{code}/nc.shtml", target: '_blank', rel: "noopener noreferrer"
            end
            column :symbol do |stock|
              link_to "#{stock.symbol}", admin_stock_path(stock.id)
            end
            column :ch do |stock|
              (JSON.parse $Redis.get(stock.symbol))["changepercent"].round(1)
            end
            column :yc do |stock|
              stock.year_count
            end
            column :vch do |stock|
              stock.volume_change
            end
            column :flow do |stock|
              link_to "#{stock.ths_flow}", "https://stockpage.10jqka.com.cn/#{stock.symbol}/funds/#funds_sszjlx", target: '_blank', rel: "noopener noreferrer"
            end
            column :block do |stock|
              "#{stock.blocks.pluck(:name) & gn_base50_name}" + "#{stock.blocks.pluck(:name) & hy_base50_name}"
            end
          end
        end
      end
      column do
        panel "涨幅 大于 5%" do
          table_for stock_up5.reverse do
            column '【name】' do |stock|
              code = %w(5 6 9).include?(stock.symbol[0]) ? "sh#{stock.symbol}" : "sz#{stock.symbol}"
              link_to "#{stock.name}", "https://finance.sina.com.cn/realstock/company/#{code}/nc.shtml", target: '_blank', rel: "noopener noreferrer"
            end
            column :symbol do |stock|
              link_to "#{stock.symbol}", admin_stock_path(stock.id)
            end
            column :ch do |stock|
              (JSON.parse $Redis.get(stock.symbol))["changepercent"].round(1)
            end
            column :yc do |stock|
              stock.year_count
            end
            column :vch do |stock|
              stock.volume_change
            end
            column :flow do |stock|
              link_to "#{stock.ths_flow}", "https://stockpage.10jqka.com.cn/#{stock.symbol}/funds/#funds_sszjlx", target: '_blank', rel: "noopener noreferrer"
            end
            column :block do |stock|
              "#{stock.blocks.pluck(:name) & gn_base50_name}" + "#{stock.blocks.pluck(:name) & hy_base50_name}"
            end
          end
        end
      end
    end
  end
end